Git Kata 6: Remote Repositories
Learn how to create and add remote repositories and push commits to the repository.
The Git katas have, thus far, worked with a local repository. Git has a rich feature set that allows repositories to send and retrieve commits to and from each other, sharing the entire commit history of one repository with an infinite number of other repositories. This kata will demonstrate the steps necessary to connect a local repository to a remote repository.
Step 1: Initialize a new repository#
The command to change the directory and list all the files is given below.
Commands
Command / Parameter | Description |
| This changes to the |
| This lists all the files, including the hidden files and directories. |
The web-storelist directory holds an HTML-formatted version of storelist.txt. This will be the content used to publish the grocery list to the web. The ls -A command shows that there’s no hidden .git directory. The web-storelist directory isn’t yet a Git repository.
The command to initialize a new repository is given below:
Command's Parameter
Command / Parameter | Description |
| This initializes a new repository. |
The git init command creates a new repository in web-storelist.
The commands for repository configuration are given below.
Command's Parameters
Command / Parameter | Description |
| This sets a configuration value that affects the behavior of the Git program. |
| These values set the user name and email identity associated with one or more repositories. |
The git config command is used in this step to set the identity for this Git repository.
The command to stage the files is given below.
Command's Parameters
Command / Parameter | Description |
| This stage changes the working directory to the index. Staged changes will be included in the next commit. |
| This stages all the modified files. |
Recall that git init only creates a repository but doesn’t stage any files or create any commits. The git add command stages files in the working directory, making them ready to be included in the next commit. Remember also that the files in web-storelist are untracked after the repository is created. There are three files in web-storelist, so the --all option stages all three untracked files with one command.
The command to display the status of the repository is given below.
Command's Parameter
Command / Parameter | Description |
| This displays the status of the files in the working directory of the repository. |
The git status command displays the status of the files in web-storelist. The three files in web-storelist are listed as new, staged, and ready for commit.
The output of this command is:
Output
Message | Meaning |
On branch master | The current branch is |
Initial commit | The next commit will be the first commit in this repository. |
| The list following this message is the changes that will be committed. |
| The three changes listed above will be committed. All three files in the working directory have been added. They will be included in the next commit, which will also make them tracked. |
The command to commit the changes to the repository is given below:
Command's Parameters
Command / Parameter | Description |
| This creates a new commit in the repository. |
| This stages all the modified files prior to the commit. |
| The |
| This is the commit message. |
This step creates the first commit in the local web-storelist repository.
The output of this command is:
Output
Message | Meaning |
"[master (root-commit) c3ae04e] Initial Commit" | The commit was on the |
"3 files changed, 0 insertions(+)" | This is a summary of the changes included in the commit. |
"create mode 100644 store.png" "create mode 100644 storelist.htm" "create mode 100644 style.css" | This is a list of all the changes committed. The “create" mode indicates the type of file and its permissions. |
Step 2: Add a remote repository#
The command to add a Gogs repository as a remote is given below.
Command's Parameters
Command / Parameter | Description |
| The |
| This indicates that this command will add a new remote to the list of remotes for this repository. A repository can have more than one remote repository set. |
| This is a name assigned to the remote. By convention, the primary (and often the only) remote is usually named |
| This is the URL to the remote repository, which is hosted by the Gogs server we ran in the previous kata. |
The git remote add command adds a new remote repository to the local web-storelist repository. That is the first step in sharing the commit history between the local repository and another repository.
The URL indicates the path to the empty web-storelist repository created in the Gogs server in the previous kata.
The command to list all the remotes is given below.
Command's Parameters
Command / Parameter | Description |
| The remote command is used to add, remove, and modify remote repositories to a repository. |
| This parameter lists all the remotes registered in the current repository. |
The git remote -v command lists all the remotes added to the local web-storelist repository. There are two identical URLs, one for fetch and one for push. The fetch and push are Git commands we’ll see in a future kata.
The output of the command is:
Output
Message | Meaning |
origin https://ed-6091404232622080:3000/devops/web-storelist (fetch) | This is the URL that will be used by Git for the |
origin https://ed-6091404232622080:3000/devops/web-storelist (fetch) | This is the URL that will be used by Git for the |
Step 3: Push commits to a remote repository#
The commands to push the commits are given below.
- Enter “devops” for the username.
- Enter “katas” for the password.
Command's Parameters
Command / Parameter | Description |
| This sends commits on the designated branch to a remote repository. |
| The `-u` parameter is short for |
| This is the name of the remote to which the pushed commits will be sent. |
| This is the name of the local branch from which the commits are sent. |
The git push command sends commits in a local branch to a remote repository designated in the command. Git can push from any repository to any repository accessible by disk or network. Developers typically use a Git server to store a shared codebase. Git servers are also used as the entry point to continuous integration pipelines. This command pushes the local commits in main to the Gogs server we started in the previous Git kata.
This execution creates a new main branch in the remote and sets up a local tracking branch (called origin/main) that’s used to track the differences between the local and remote main branches.
The output of this command is:
Output
Message | Meaning |
Counting objects: 100% (3/3), done. | Git counts the number of objects to be sent to the remote repository. |
Compressing objects: 100% (2/2), done. | Git compresses objects prior to sending them to the remote, to speed transmission. |
Writing objects 100% (3/), 234 bytes | 234.00 KiB/s, done. | Git writes the objects to the remote. |
Total 3 (delta 0), reused 0 (delta 0) | A summary of the files sent. |
To http://localhost:3000 | The URL of the remote to which the commits were sent. |
* [new branch] master -> master | This indicates that a new remote branch was created as a result of the command. The |
The command to display the status of the repository is given below.
Command's Parameter
Command / Parameter | Description |
| This displays the status of the files in the working directory of the repository. |
This execution of git status shows that there are no uncommitted changes in the working directory. There’s an additional line we haven’t seen before, indicating that the local main branch is up-to-date with the tracking branch origin/main. This is the local reference branch we created with the git push -u command earlier.
Pushes to origin from other repositories or new “un-pushed” commits to the local main branch will cause our local main branch to be “ahead” or “behind” the remote. New local commits can be pushed, bringing the local main branch back into synchronization with the remote. Commits to the remote repository from other repositories can be retrieved using git pull, which we’ll see in a later step.
The output of this command is:
Output
Message | Meaning |
On branch master | The current branch is |
Your branch is up-to-date with ‘origin/master’ | The commit history of the local |
nothing to commit, working tree clean | The index of the local repository matches the working directory. There are no unstaged or uncommitted changes. |
To view the repository in Gogs:
- Switch to the browser (Gogs).
- Click “Dashboard” at the top.
- Select the “web-storelist” repository under “My Repositories” on the right.
The git push command has populated the web-storelist repository with the files in the web-storelist local repository. The commit history of this repository can be viewed by clicking the “Commits” link at the center-left. The one commit listed is the one pushed in the previous step.
Git Kata 5: Run a Git Server
Git Kata 7: Collaboration as Ken